
#ifndef SESSION_H_
#define SESSION_H_

#ifdef __cplusplus
extern "C" {
#endif

#define IPADDR_LEN		128

#include "callback.h"
#include "fastArray.h"

#define INVALID_ID		-1
#define INVALID_FD		-1
#define	NO_USE				-1

struct arrayItem;
struct thread;
struct buffer;

typedef enum sendEvent {
	CLOSE_SENDEVENT	=	0,
	OPEN_SENDEVENT	=	1,
} sendEvent;

typedef enum sessionType {
	NOMAL_SESSIONTYPE			=	0,
	CONNECTOR_SESSIONTYPE	=	1,
} sessionType;

typedef struct session {
	struct arrayItem arrayItem;
	int id;
	int cfd;
	int cport;
	char cip[IPADDR_LEN];
	int inUse;

	char* conAddr;
	int conPort;
	enum sessionType sessionType;

	struct buffer* readBuffer;
	struct buffer* sendBuffer;
	sendEvent sendEventState;

	struct thread* t;
	IServerCallBack* callBackHandle;
	void* server;
    void* privateData;
    void* interData;
} session;

struct session* initSession();
void destroySession(struct session* session);
// no thread safe!!!
int readSession(struct session* session);
// no thread safe!!!
int writeSession(struct session* session, char* sendBuf, int sendBufLen);
// thread safe!!!
int writeSessionEx(struct session* session, char* sendBuf, int sendBufLen);
void shutdownSession(struct session* session);

#ifdef __cplusplus
}
#endif

#endif /* SESSION_H_ */
